package com.woniu.user.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.woniu.common.entity.ResponseResult;
import com.woniu.common.entity.User;
import com.woniu.user.entity.Perm;
import com.woniu.user.entity.SociaUser;
import com.woniu.user.service.UserService;
import com.woniu.user.utils.HttpUtils;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class LoginController{
    @Autowired
    UserService userService;

    @RequestMapping("/login")
    public ResponseResult Login(User user) {
        System.out.println(user);
        ResponseResult responseResult = new ResponseResult();
        User user1 = userService.findUserByAccont(user.getAccount());
        if (user1 == null) {
            responseResult.setCode(400);
            responseResult.setMessage("账号或者密码错误");
            return responseResult;
        }
        if (user.getPassword().equals(user1.getPassword())) {
            List<Perm> listAllPerms = userService.queryAllMenu(user1.getId());
            responseResult.setData(user1);
            responseResult.setRights(listAllPerms);
            responseResult.setCode(200);
            responseResult.setMessage("登录成功");
            return responseResult;
        } else {
            responseResult.setCode(400);
            responseResult.setMessage("账号或者密码错误");
            return responseResult;
        }
    }

    @RequestMapping("/weibologin")
    public void weiboLogin(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String code =  request.getParameter("code");
        String url = "https://api.weibo.com/oauth2/access_token";
        Map<String,String> map =new HashMap<>();
        HttpSession session = request.getSession();
        map.put("client_id","3069540542");
        map.put("client_secret","82f71e79ad29118747659deb5771344e");
        map.put("grant_type","authorization_code");
        map.put("redirect_uri","http://pet.com:8080/weibologin");
        map.put("code",code);
        HttpResponse resp = HttpUtils.doPost(url,map);
        System.out.println(resp);
        ResponseResult<Object> responseResult = new ResponseResult<>();
        if(resp.getStatusLine().getStatusCode()==200){
            //获取到了access_token
            String json = EntityUtils.toString(resp.getEntity());
            SociaUser sociaUser = JSON.parseObject(json, SociaUser.class);
            User user=userService.findUserByAccont(sociaUser.getUid());
            //如果等于空,则插入一条数据
            //1.当前用户如果是第一次这个网站,就自动注册(为当前社交用户生成一个账号)
            if(user==null){
                if(userService.insertAccount(sociaUser.getUid())){
                    User user1=userService.findUserByAccont(sociaUser.getUid());
                    userService.addPersonalPerm(user1.getId());
                    userService.addHospitalPerm(user1.getId());
                    userService.addPolicePerm(user1.getId());
                    List<Perm> listAllPerms=userService.queryAllMenu(user1.getId());
                    responseResult.setData(user1);
                    responseResult.setRights(listAllPerms);
                    response.setContentType("application/json;charset=utf-8");
                    responseResult.setCode(200);
                    responseResult.setMessage("登录成功");
                }
            }else {
                List<Perm> listAllPerms=userService.queryAllMenu(user.getId());
                //            UserService userService = new UserServiceImpl();
//            User weibouser = userService.weibologin(sociaUser.getUid());
                //第一次使用Session,命令浏览器保存卡号 JSESSSIONID 这个cookie
                //以后浏览器访问哪个网站就会带上这个网站的cookie
                //发卡的时候(指定域名为父域名),即使是子域发的卡,父域也可以使用
                //TODB 1> 默认发的令牌  . session =dsajkdjl.作用域:当前域 (解决子域共享问题
                // TODB 2> 使用json序列化方式来序列化对象数据到redis中
//            session.setAttribute("userid",weibouser.getId());
//	            servletResponse.addCookie(new Cookie("JSESSSIONID","userInfo"));
                responseResult.setData(user);
                responseResult.setRights(listAllPerms);
                response.setContentType("application/json;charset=utf-8");
                responseResult.setCode(200);
                responseResult.setMessage("登录成功");
            }
            //返回结果
        }else {
            response.setContentType("application/json;charset=utf-8");
            responseResult.setCode(400);
            responseResult.setMessage("系统繁忙,请稍后登录");
        }
        response.getWriter().write(JSONObject.toJSONString(responseResult));
    }

}
